<script setup lang="ts">
import type { Message,Prescription } from '@/types/room';
import { IllnessTime, MsgType } from '@/types/enum';
import { flagOptions, timeOptions } from '@/api/constants'
import { Toast, showImagePreview } from 'vant'

import { PrescriptionStatus } from '@/types/enum';
import { useRouter } from 'vue-router'
import EvaluateCard from './EvaluateCard.vue'
import { useLookPre } from '@/hooks';
defineProps<{
  list:Message[]
}>()
// 获取患者患病时间的lebel信息
const getIllnessTimeLabel = (time:IllnessTime)=>{
    return timeOptions.find(item=>item.value === time)?.label
}
// 获取患者是否就诊过的label信息
const getFlagLabel = (flag:0|1)=>{
    return flagOptions.find(item=>item.value === flag)?.label
}
// 预览患者上传的图片
import type { Image } from '@/types/consult';
import { useUserStore } from '@/stores';
import dayjs from 'dayjs';

const previewImg = (pictures?: Image[]) => {
  if (pictures && pictures.length){
    showImagePreview(pictures.map((item) => item.url))
  } else{
    console.log("No images")
  }
}
const formatTime = (time: string) => dayjs(time).format('HH:mm')
const store = useUserStore()
const loadSuccess = () => {
  window.scrollTo(0, document.body.scrollHeight)
}
// 查看处方

const router = useRouter()
// 点击购买药品
const goPay = (prescription?:Prescription) => {
  if(!prescription) return
  // 1.处方失效
  if(prescription.status === PrescriptionStatus.Invalid){
    console.log("处方已失效")
    Toast.fail('处方已失效')
    return
  } else if(prescription.status === PrescriptionStatus.NotPayment){
    // 2.处方未支付
    console.log("处方未支付")
    return router.push(`/medicine/pay?id=${prescription.id}`)
  } else if(prescription.status === PrescriptionStatus.Payment){
    // 3.处方已支付
    console.log("处方已支付")
    return
  }
}
const {lookPre} = useLookPre() 
</script>

<template>
   <template v-for="{ msgType, msg, id,createTime,from,fromAvatar } in list" :key="id">
  <!-- 1. 病情描述 -->
  <div class="msg msg-illness" v-if="msgType === MsgType.CardPat">
    <div class="patient van-hairline--bottom" v-if="msg.consultRecord">
      <p>
        {{ msg.consultRecord.patientInfo.name }}
        {{ msg.consultRecord.patientInfo.genderValue }}
        {{ msg.consultRecord.patientInfo.age }}岁
      </p>
      <p>
        {{ getIllnessTimeLabel(msg.consultRecord.illnessTime) }} |
        {{ getFlagLabel(msg.consultRecord.consultFlag) }}
      </p>
    </div>
    <van-row>
      <van-col span="6">病情描述</van-col>
      <van-col span="18">{{ msg.consultRecord?.illnessDesc }}</van-col>
      <van-col span="6">图片</van-col>
      <van-col span="18" @click="previewImg(msg.consultRecord?.pictures)"> 点击查看 </van-col>
    </van-row>
  </div>
  <!--2.  温馨提示 -->
  <div class="msg msg-tip" v-if="msgType === MsgType.NotifyTip">
    <div class="content">
      <span class="green">温馨提示：</span>
      <span>{{ msg.content }}</span>
    </div>
  </div>
  <!-- 3. 通用通知 -->
  <div class="msg msg-tip" v-if="msgType === MsgType.Notify">
    <div class="content">
      <span>{{ msg.content }}</span>
    </div>
  </div>
  <!--4.患者发的消息-->
  <div class="msg msg-to" v-if="msgType === MsgType.MsgText && store.user?.id === from">
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
      <van-image :src="store.user?.avatar" />
    </div>
    <!-- 接收文字：医生发的消息 -->
    <div class="msg msg-from" v-if="msgType === MsgType.MsgText && store.user?.id !== from">
      <van-image :src="fromAvatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <div class="pao">{{ msg.content }}</div>
      </div>
    </div>
     <!-- 处方卡片 -->
  <div class="msg msg-recipe" v-if="msgType === MsgType.CardPre&&store.user?.id !== from">
    <div class="content">
      <div class="head van-hairline--bottom">
        <div class="head-tit">
          <h3>电子处方</h3>
            <p @click="lookPre(msg.prescription?.id ?? '')">原始处方 <van-icon name="arrow"></van-icon></p>
        </div>
        <p>{{ msg.prescription?.name }} {{ msg.prescription?.genderValue }} {{msg.prescription?.age}}岁 {{ msg.prescription?.diagnosis }}</p>
        <p>{{ msg.prescription?.createTime }}</p>
      </div>
      <div class="body">
        <div class="body-item" v-for="i in msg.prescription?.medicines" :key="i.id">
          <div class="durg">
            <p>{{ i.name }} {{ i.specs }}</p>
            <p>{{ i.usageDosag }}</p>
          </div>
          <div class="num">x{{ i.quantity }}</div>
        </div>
      </div>
      <div class="foot" @click="goPay(msg.prescription)"><span>购买药品</span></div>
    </div>
  </div>
  <!--图片消息-->
  <div class="msg msg-to" v-if="msgType === MsgType.MsgImage && store.user?.id === from">
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image fit="contain" :src="msg.picture?.url" @load="loadSuccess"/>
      </div>
      <van-image :src="store.user?.avatar" />
    </div>
    <div class="msg msg-from" v-if="msgType === MsgType.MsgImage && store.user?.id !== from">
      <van-image :src="fromAvatar" />
      <div class="content">
        <div class="time">{{ formatTime(createTime) }}</div>
        <van-image fit="contain" :src="msg.picture?.url" @load="loadSuccess"/>
      </div>
    </div>
    <!--评价组件-->
    <div
      class="msg msg-comment"
      v-if="msgType === MsgType.CardEva || msgType === MsgType.CardEvaForm"
    >
      <evaluate-card :evaluateDoc="msg.evaluateDoc" />
    </div>
    <!--结束问诊消息-->
    <div class="msg msg-tip msg-tip-cancel" v-if="msgType === MsgType.NotifyCancel">
      <div class="content">
        <span>{{ msg.content }}</span>
      </div>
    </div>
</template> 
</template>

<style lang="scss" scoped>
@import '@/styles/room.scss';
</style>
